/* $RCSFile: UpdateContact.ascx.cs $
 * Copyright (C) Addon Nice Technologies 2010
 * $Source: ?/Addon Nice/AddonNice/AddonNice.LeadProspection/PageModules/UpdateContact/UpdateContact.ascx.cs $
 * $log$
 * Revision 24 2010/08/09 11:16:26  christian.surieux
 *   Added Log comment to all .ascx .aspx .master files
 * Revision 11 2010/08/09 10:18:24  christian.surieux
 *   Added Log comment to all cs files
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;  
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.Drawing;
using System.Data.SqlClient;
using System.Web.Services.Protocols;
using System.Globalization;

using AddonNice.UI;
using AddonNice.Design;
using AddonNice.UI.WebControls;
using AddonNice.Security;
using AddonNice.UI.DataTypes;
using AddonNice.Configuration;
using AddonNice.Configuration.Settings;
using AddonNice.Diagnostics;
using AddonNice.Helpers;
using AddonNice.Settings;

using AddonNice.CRMWrapper;
using CrmSdk;

using Trace2    =   System.Diagnostics.Trace;
using AddonNice.LeadProspection.Settings;

namespace AddonNice.PageModules.UpdateContact
{
    public class AStr
    {
        public const string EmailMandatory      =   "CONTACT_INPUT_EMAIL";
    }

    /// <summary>
    /// Initializer class for UpdateContact 
    /// </summary>
    public class UpdateContactSI : PortalCRMModuleControlSI
    {
        public UpdateContactSI(ModuleSettingsDictionary bSettings,ISettingsOwner pm) : base()
        {
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]UpdateContactSI Ctor BEG.",ModuleTraceSwitch.Sw.Info);
            InitSettings(bSettings,pm);
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]UpdateContactSI Ctor END.",ModuleTraceSwitch.Sw.Info);
        }
 
        public override void InitSettings(ModuleSettingsDictionary bSettings,ISettingsOwner pm)
        {             
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]UpdateContactSI InitSettings BEG.",ModuleTraceSwitch.Sw.Info);
            
            base.InitSettings(bSettings,pm);


			// Specific parameters
           //const string extgrp                     =   "UPDATECONTACT";

            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]UpdateContactSI InitSettings END.",ModuleTraceSwitch.Sw.Info);
        }
        
    }

    public partial class UpdateContact : PortalCRMModuleControl
    {
        
        AddonNiceUserDB userDB  = new AddonNiceUserDB();

        #region General implementation

        public override Guid GuidID
        {
            get
            {
                return new Guid("{4A5BBF29-F67E-4925-B154-2EB2F76FA759}");
            }
        }
        #endregion General implementation

        /// <summary>
        /// constructor
        /// </summary>
        public UpdateContact()
        {
            // Validators : name Ok
            ValidationGroup                         =   "Main";
        }

        public override void InitPortalModuleControl(ModuleSettingsDictionary bSettings)
        {
            PortalModuleInitializer =   new UpdateContactSI(bSettings,this);
        }

        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]UpdateContact Page_Load isPostBack: {0}",IsPostBack),ModuleTraceSwitch.Sw.Info);
            if (!IsPostBack)
            {
                if ( !IsUnderCRM )
                    return;
                
                if ( IsInEntityCreationMode )
                    return;
                try
                {
                    ADNDynInterview dynD =   DynEntity.GetFromCache<ADNDynInterview>(PStr.interview,Wrapper,ResetWrapper,EntityID);
                    if ( dynD.Retrieved )
                    {
                        ADNDynContact dynC  =   DynEntity.GetFromCache<ADNDynContact>(CrmStr.contact,Wrapper,ResetWrapper,dynD.contactid);
                        if ( dynC.Retrieved )
                        {
                            TbContactTel.Text   =   dynC.telephone1;
                            TbContactEMail.Text =   dynC.emailaddress1;
                        }
                    }

                }
                catch (Exception ex)
                {
                    if (ModuleTraceSwitch.Sw.TraceError)
                        Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]UpdateContact Page_Load GetAllCivilities ex: {0}", ex),ModuleTraceSwitch.Sw.Info);
                    throw;
                }
            }
        }

        void UpdateContact_ADNUpdateModule(object sender, ModuleEventArgs e)
        {
            try
            {
                    ADNDynInterview dynD        =   DynEntity.GetFromCache<ADNDynInterview>(PStr.interview,Wrapper,ResetWrapper,EntityID);
                    if ( dynD.Retrieved )
                    {
                        ADNDynContact dynC      =   DynEntity.GetFromCache<ADNDynContact>(CrmStr.contact,Wrapper,ResetWrapper,dynD.contactid);
                        if ( dynC.Retrieved )
                        {
                            dynC.telephone1     =   TbContactTel.Text;
                            dynC.emailaddress1  =   TbContactEMail.Text;
                        }
                    }
            }
            catch (Exception ex)
            {
                if (ModuleTraceSwitch.Sw.TraceError)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"UpdateContact UpdateContact_ADNUpdateModule ex :{0}", ex),ModuleTraceSwitch.Sw.Info);
                throw;
            }
        }
        
        
        public override void Page_PreInit(object sender, EventArgs e)
        {
            updateBtn.SkinID    =   "Update_" + Helpers.LocalizeHelper.GetCurrentNeutralCultureName();
            base.Page_PreInit(sender, e);
        }

        #region Std overrides

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            Load            +=  new System.EventHandler(this.Page_Load);
            ADNUpdateModule +=  new PortalModuleEventHandler(UpdateContact_ADNUpdateModule);
        }
        
        #endregion Std overrides
    }
}
